home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jsgc.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-08  |  9.6 KB  |  270 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef jsgc_h___
  41. #define jsgc_h___
  42. /*
  43.  * JS Garbage Collector.
  44.  */
  45. #include "jsprvtd.h"
  46. #include "jspubtd.h"
  47. #include "jsdhash.h"
  48.  
  49. JS_BEGIN_EXTERN_C
  50.  
  51. /* GC thing type indexes. */
  52. #define GCX_OBJECT              0               /* JSObject */
  53. #define GCX_STRING              1               /* JSString */
  54. #define GCX_DOUBLE              2               /* jsdouble */
  55. #define GCX_MUTABLE_STRING      3               /* JSString that's mutable --
  56.                                                    single-threaded only! */
  57. #define GCX_PRIVATE             4               /* private (unscanned) data */
  58. #define GCX_NAMESPACE           5               /* JSXMLNamespace */
  59. #define GCX_QNAME               6               /* JSXMLQName */
  60. #define GCX_XML                 7               /* JSXML */
  61. #define GCX_EXTERNAL_STRING     8               /* JSString w/ external chars */
  62.  
  63. #define GCX_NTYPES_LOG2         4               /* type index bits */
  64. #define GCX_NTYPES              JS_BIT(GCX_NTYPES_LOG2)
  65.  
  66. /* GC flag definitions, must fit in 8 bits (type index goes in the low bits). */
  67. #define GCF_TYPEMASK    JS_BITMASK(GCX_NTYPES_LOG2)
  68. #define GCF_MARK        JS_BIT(GCX_NTYPES_LOG2)
  69. #define GCF_FINAL       JS_BIT(GCX_NTYPES_LOG2 + 1)
  70. #define GCF_SYSTEM      JS_BIT(GCX_NTYPES_LOG2 + 2)
  71. #define GCF_LOCKSHIFT   (GCX_NTYPES_LOG2 + 3)   /* lock bit shift */
  72. #define GCF_LOCK        JS_BIT(GCF_LOCKSHIFT)   /* lock request bit in API */
  73.  
  74. /* Pseudo-flag that modifies GCX_STRING to make GCX_MUTABLE_STRING. */
  75. #define GCF_MUTABLE     2
  76.  
  77. #if (GCX_STRING | GCF_MUTABLE) != GCX_MUTABLE_STRING
  78. # error "mutable string type index botch!"
  79. #endif
  80.  
  81. extern uint8 *
  82. js_GetGCThingFlags(void *thing);
  83.  
  84. /* These are compatible with JSDHashEntryStub. */
  85. struct JSGCRootHashEntry {
  86.     JSDHashEntryHdr hdr;
  87.     void            *root;
  88.     const char      *name;
  89. };
  90.  
  91. struct JSGCLockHashEntry {
  92.     JSDHashEntryHdr hdr;
  93.     const JSGCThing *thing;
  94.     uint32          count;
  95. };
  96.  
  97. #if 1
  98. /*
  99.  * Since we're forcing a GC from JS_GC anyway, don't bother wasting cycles
  100.  * loading oldval.  XXX remove implied force, fix jsinterp.c's "second arg
  101.  * ignored", etc.
  102.  */
  103. #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JS_TRUE)
  104. #else
  105. #define GC_POKE(cx, oldval) ((cx)->runtime->gcPoke = JSVAL_IS_GCTHING(oldval))
  106. #endif
  107.  
  108. extern intN
  109. js_ChangeExternalStringFinalizer(JSStringFinalizeOp oldop,
  110.                                  JSStringFinalizeOp newop);
  111.  
  112. extern JSBool
  113. js_InitGC(JSRuntime *rt, uint32 maxbytes);
  114.  
  115. extern void
  116. js_FinishGC(JSRuntime *rt);
  117.  
  118. extern JSBool
  119. js_AddRoot(JSContext *cx, void *rp, const char *name);
  120.  
  121. extern JSBool
  122. js_AddRootRT(JSRuntime *rt, void *rp, const char *name);
  123.  
  124. extern JSBool
  125. js_RemoveRoot(JSRuntime *rt, void *rp);
  126.  
  127. /*
  128.  * The private JSGCThing struct, which describes a gcFreeList element.
  129.  */
  130. struct JSGCThing {
  131.     JSGCThing   *next;
  132.     uint8       *flagp;
  133. };
  134.  
  135. #define GC_NBYTES_MAX           (10 * sizeof(JSGCThing))
  136. #define GC_NUM_FREELISTS        (GC_NBYTES_MAX / sizeof(JSGCThing))
  137. #define GC_FREELIST_NBYTES(i)   (((i) + 1) * sizeof(JSGCThing))
  138. #define GC_FREELIST_INDEX(n)    (((n) / sizeof(JSGCThing)) - 1)
  139.  
  140. extern void *
  141. js_NewGCThing(JSContext *cx, uintN flags, size_t nbytes);
  142.  
  143. extern JSBool
  144. js_LockGCThing(JSContext *cx, void *thing);
  145.  
  146. extern JSBool
  147. js_LockGCThingRT(JSRuntime *rt, void *thing);
  148.  
  149. extern JSBool
  150. js_UnlockGCThingRT(JSRuntime *rt, void *thing);
  151.  
  152. extern JSBool
  153. js_IsAboutToBeFinalized(JSContext *cx, void *thing);
  154.  
  155. extern void
  156. js_MarkAtom(JSContext *cx, JSAtom *atom, void *arg);
  157.  
  158. /* We avoid a large number of unnecessary calls by doing the flag check first */
  159. #define GC_MARK_ATOM(cx, atom, arg)                                           \
  160.     JS_BEGIN_MACRO                                                            \
  161.         if (!((atom)->flags & ATOM_MARK))                                     \
  162.             js_MarkAtom(cx, atom, arg);                                       \
  163.     JS_END_MACRO
  164.  
  165. extern void
  166. js_MarkGCThing(JSContext *cx, void *thing, void *arg);
  167.  
  168. #ifdef GC_MARK_DEBUG
  169.  
  170. typedef struct GCMarkNode GCMarkNode;
  171.  
  172. struct GCMarkNode {
  173.     void        *thing;
  174.     const char  *name;
  175.     GCMarkNode  *next;
  176.     GCMarkNode  *prev;
  177. };
  178.  
  179. #define GC_MARK(cx_, thing_, name_, prev_)                                    \
  180.     JS_BEGIN_MACRO                                                            \
  181.         GCMarkNode node_;                                                     \
  182.         node_.thing = thing_;                                                 \
  183.         node_.name  = name_;                                                  \
  184.         node_.next  = NULL;                                                   \
  185.         node_.prev  = prev_;                                                  \
  186.         if (prev_) ((GCMarkNode *)(prev_))->next = &node_;                    \
  187.         js_MarkGCThing(cx_, thing_, &node_);                                  \
  188.     JS_END_MACRO
  189.  
  190. #else  /* !GC_MARK_DEBUG */
  191.  
  192. #define GC_MARK(cx, thing, name, prev)   js_MarkGCThing(cx, thing, NULL)
  193.  
  194. #endif /* !GC_MARK_DEBUG */
  195.  
  196. /*
  197.  * Flags to modify how a GC marks and sweeps:
  198.  *   GC_KEEP_ATOMS      Don't sweep unmarked atoms, they may be in use by the
  199.  *                      compiler, or by an API function that calls js_Atomize,
  200.  *                      when the GC is called from js_NewGCThing, due to a
  201.  *                      malloc failure or the runtime GC-thing limit.
  202.  *   GC_LAST_CONTEXT    Called from js_DestroyContext for last JSContext in a
  203.  *                      JSRuntime, when it is imperative that rt->gcPoke gets
  204.  *                      cleared early in js_GC, if it is set.
  205.  *   GC_ALREADY_LOCKED  rt->gcLock is already held on entry to js_GC, and kept
  206.  *                      on return to its caller.
  207.  */
  208. #define GC_KEEP_ATOMS       0x1
  209. #define GC_LAST_CONTEXT     0x2
  210. #define GC_ALREADY_LOCKED   0x4
  211.  
  212. extern void
  213. js_ForceGC(JSContext *cx, uintN gcflags);
  214.  
  215. extern void
  216. js_GC(JSContext *cx, uintN gcflags);
  217.  
  218. #ifdef DEBUG_notme
  219. #define JS_GCMETER 1
  220. #endif
  221.  
  222. #ifdef JS_GCMETER
  223.  
  224. typedef struct JSGCStats {
  225.     uint32  alloc;      /* number of allocation attempts */
  226.     uint32  freelen[GC_NUM_FREELISTS];
  227.                         /* gcFreeList lengths */
  228.     uint32  recycle[GC_NUM_FREELISTS];
  229.                         /* number of things recycled through gcFreeList */
  230.     uint32  retry;      /* allocation attempt retries after running the GC */
  231.     uint32  retryhalt;  /* allocation retries halted by the branch callback */
  232.     uint32  fail;       /* allocation failures */
  233.     uint32  finalfail;  /* finalizer calls allocator failures */
  234.     uint32  lockborn;   /* things born locked */
  235.     uint32  lock;       /* valid lock calls */
  236.     uint32  unlock;     /* valid unlock calls */
  237.     uint32  depth;      /* mark tail recursion depth */
  238.     uint32  maxdepth;   /* maximum mark tail recursion depth */
  239.     uint32  cdepth;     /* mark recursion depth of C functions */
  240.     uint32  maxcdepth;  /* maximum mark recursion depth of C functions */
  241.     uint32  dswmark;    /* mark C stack overflows => Deutsch-Schorr-Waite */
  242.     uint32  dswdepth;   /* DSW mark depth */
  243.     uint32  maxdswdepth;/* maximum DSW mark depth */
  244.     uint32  dswup;      /* DSW moves up the mark spanning tree */
  245.     uint32  dswupstep;  /* steps in obj->slots to find DSW-reversed pointer */
  246.     uint32  maxlevel;   /* maximum GC nesting (indirect recursion) level */
  247.     uint32  poke;       /* number of potentially useful GC calls */
  248.     uint32  nopoke;     /* useless GC calls where js_PokeGC was not set */
  249.     uint32  afree;      /* thing arenas freed so far */
  250.     uint32  stackseg;   /* total extraordinary stack segments scanned */
  251.     uint32  segslots;   /* total stack segment jsval slots scanned */
  252. } JSGCStats;
  253.  
  254. extern JS_FRIEND_API(void)
  255. js_DumpGCStats(JSRuntime *rt, FILE *fp);
  256.  
  257. #endif /* JS_GCMETER */
  258.  
  259. #ifdef DEBUG_notme
  260. #define TOO_MUCH_GC 1
  261. #endif
  262.  
  263. #ifdef WAY_TOO_MUCH_GC
  264. #define TOO_MUCH_GC 1
  265. #endif
  266.  
  267. JS_END_EXTERN_C
  268.  
  269. #endif /* jsgc_h___ */
  270.